/*
 * @lc app=leetcode.cn id=997 lang=typescript
 *
 * [997] 找到小镇的法官
 */

// @lc code=start
function findJudge(n: number, trust: number[][]): number {
    // if (trust.length === 0 && n === 1) {
    //     return 1
    // }
    // let set = new Set([...new Array(n).keys()]);
    // let judge = -1;
    // let map = new Map<number, number[]>();
    // for (let i = 0; i < trust.length; i++) {
    //     const value = map.get(trust[i][0]) || [];
    //     value.push(trust[i][1])
    //     map.set(trust[i][0], value)
    //     set.delete(trust[i][0] - 1);
    // }
    // let flag = false;
    // if (set.size === 1) {
    //     const v = Array.from(set.keys())[0] + 1;
    //     judge = v;
    //     console.log('v', v)
    //     for (const [key, value] of map) {
    //         flag = value.some(r => r === v)
    //         if (!flag) {
    //             break;
    //         }
    //     }
    // }
    // judge = flag ? judge : -1;
    // return map.size + 1 === n ? judge : -1;

    const inDegree = new Array(n + 1).fill(0);
    const outDegree = new Array(n + 1).fill(0);
    for (const edge of trust) {
        const x = edge[0], y = edge[1];
        ++inDegree[y];
        ++outDegree[x];
    }
    console.log(inDegree, outDegree)
    for (let i = 1; i <= n; ++i) {
        console.log(i, n - 1);
        if (inDegree[i] === n - 1 && outDegree[i] === 0) {
            console.log(i);
            return i;
        }
    }
    console.log(-1);
    return -1;
}

findJudge(3, [[1, 3], [2, 3], [3, 1]])
// @lc code=end
